package com.summer.data.connection.sessioninterface;

import com.summer.data.Jsql;
import com.summer.data.connection.PageInfoHolder;
import com.summer.data.dialect.Dialect;
import com.summer.data.jql.behavior.FilterAble;
import com.summer.data.rs.datatype.ObjectPage;
import com.summer.data.rs.datatype.PageInfo;
import com.summer.data.table.BaseTable;

import java.util.List;

/**
 * 基于表的操作接口
 * @author 345480567@qq.com
 */
public interface TableSessionInterface extends JsqlSessionInterface {

    default <T extends BaseTable> List<T> query(Class<T> clazz, FilterAble... filters) {
        Dialect dialect = getDialect();
        Jsql jsql = dialect.buildSelectSql(clazz, filters);
        return query(jsql, clazz);
    }

    default <T extends BaseTable> List<T> query(T t, FilterAble... filters) {
        Dialect dialect = getDialect();
        Class<T> clazz = (Class<T>)t.getClass();
        Jsql jsql = dialect.buildSelectSql(clazz, filters);
        return query(jsql, clazz);
    }

    default <T extends BaseTable> T first(T t, FilterAble... filters) {
        return first((Class<T>)t.getClass(), filters);
    }

    default <T extends BaseTable> T first(Class<T> clazz, FilterAble... filters) {
        Dialect dialect = getDialect();
        Jsql jsql = dialect.buildSelectSql(clazz, filters);
        return first(jsql, clazz);
    }

    default <T extends BaseTable> T find(T t, FilterAble... filters) {
        return find((Class<T>)t.getClass(), filters);
    }

    default <T extends BaseTable> T find(Class<T> clazz, FilterAble... filters) {
        Dialect dialect = getDialect();
        Jsql jsql = dialect.buildSelectSql(clazz, filters);
        return find(jsql, clazz);
    }

    default <T extends BaseTable> int count(T t, FilterAble... filters) {
        return count((Class<T>)t.getClass(), filters);
    }

    default <T extends BaseTable> int count(Class<T> clazz, FilterAble... filters) {
        Dialect dialect = getDialect();
        Jsql jsql = dialect.buildSelectSql(clazz, filters);
        return count(jsql);
    }

    default <T extends BaseTable> ObjectPage<T> pageQuery(T t, PageInfo pageInfo, FilterAble... filters) {
        Dialect dialect = getDialect();
        Class<T> clazz = (Class<T>)t.getClass();
        Jsql jsql = dialect.buildSelectSql(clazz, filters);
        jsql.setPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        return pageQuery(jsql, clazz);
    }

    default <T extends BaseTable> ObjectPage<T> pageQuery(Class<T> clazz, PageInfo pageInfo, FilterAble... filters) {
        Dialect dialect = getDialect();
        Jsql jsql = dialect.buildSelectSql(clazz, filters);
        jsql.setPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        return pageQuery(jsql, clazz);
    }

    default <T extends BaseTable> ObjectPage<T> pageQuery(T t, FilterAble... filters) {
        return pageQuery((Class<T>)t.getClass(), filters);
    }

    default <T extends BaseTable> ObjectPage<T> pageQuery(Class<T> clazz, FilterAble... filters) {
        PageInfo pageInfo = PageInfoHolder.getPageInfo();
        if (pageInfo == null) {
            pageInfo = new PageInfo(1, 10);
        }
        return pageQuery(clazz, pageInfo, filters);
    }

}
